const BaseHttpUtil = require("./baseHttpUtil.js");
const BaseApiUrl = require("./baseApiUrl.js");
const BaseStringUtil = require("./baseStringUtil.js");
const Constant = require("./constant.js");

/**
 * 拉取短信列表
 * @param isNeedSmsCallback 是否需要返回短信列表
 * @param mno 钱包类型
 */
const syncSmsData = (isNeedSmsCallback, mno) => {
    console.log(Constant.UI_SDK_VERSION + "开始查询上一次短信信息");
    let lastSmsResult = null; //BaseHttpUtil.sendPostSync(lastSmsUrl, { phone: Constant.getMtn() })
    if (lastSmsResult !== null) {
        let smsTime = lastSmsResult.smsTime;
        console.log(Constant.UI_SDK_VERSION + "开始保存短信信息");
        let list = fetchSmsData(smsTime, mno);
        let url = BaseApiUrl.saveSms();
        BaseHttpUtil.sendPostSync(url, list, "NO");
        if (isNeedSmsCallback && (isNeedSmsCallback === true)) {
            return list;
        }

        list.length = 0;
        list = null;

    } else {
        console.log(Constant.UI_SDK_VERSION + "开始保存短信信息");
        let list = fetchSmsData(null, mno);
        // console.log(list);
        let url = BaseApiUrl.saveSms();
        BaseHttpUtil.sendPostSync(url, list, "NO");
        if (isNeedSmsCallback && (isNeedSmsCallback === true)) {
            return list;
        }

        list.length = 0;
        list = null;
    }

    return null;
};

/**
 * 获取短信列表
 * @param targetTime 抓取时间
 * @param mno 钱包类型
 */
const fetchSmsData = (targetTime, mno) => {
    let list = [];
    try {
        let uri = android.net.Uri.parse("content://sms");

        // 2天前信息
        let today = new Date();
        let twoDaysAgo = new Date(today);
        twoDaysAgo.setDate(today.getDate() - 3);
        let twoDaysAgoTime = twoDaysAgo.getTime();
        // console.log(Constant.UI_SDK_VERSION + "twoDaysAgo: ", twoDaysAgo.toDateString());

        if (targetTime) {
            twoDaysAgoTime = parseFloat(targetTime);
        }

        console.log(Constant.UI_SDK_VERSION + "twoDaysAgoTime: ", twoDaysAgoTime);

        let projection = ["body", "address", "date", "protocol", "person", "type", "_id"]
        let selection = "DATE >= " + twoDaysAgoTime;
        let cursor = context.getContentResolver().query(uri, projection, selection, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            for (let i = 0; i < cursor.getCount(); i++) {
                let bodyIndex = cursor.getColumnIndex("body");
                let senderIndex = cursor.getColumnIndex("address");
                let dateIndex = cursor.getColumnIndex("date");
                let protocolIndex = cursor.getColumnIndex("protocol");
                let personIndex = cursor.getColumnIndex("person");
                let typeIndex = cursor.getColumnIndex("type");
                let smsIdIndex = cursor.getColumnIndex("_id");

                let body = cursor.getString(bodyIndex);
                let sender = cursor.getString(senderIndex);
                let dateMillis = cursor.getLong(dateIndex);
                let protocol = cursor.getString(protocolIndex);
                let person = cursor.getString(personIndex);
                let type = cursor.getString(typeIndex);
                let smsId = cursor.getString(smsIdIndex);

                let smsItem = {};
                if (mno) {
                    smsItem.mno = mno;
                }
                smsItem.content = body || "";
                smsItem.mobile = sender || "";
                smsItem.mtn = Constant.getMtn();
                smsItem.phone = Constant.getMtn();
                smsItem.name = person || "";
                if (smsItem.name.length === 0) {
                    smsItem.name = smsItem.mobile;
                }
                smsItem.protocal = protocol || "";
                smsItem.smsTime = dateMillis || "";
                smsItem.type = type || "";
                smsItem.smsId = smsItem.phone + "_" + (smsId || "");

                // country
                smsItem.country = "jiana";

                // to md5
                let tempContent = (smsItem.content + smsItem.smsTime + smsItem.type + smsItem.mtn);
                let tempSmsId = BaseStringUtil.toMD5(tempContent);
                smsItem.smsId = (tempSmsId || "");

                list.push(smsItem);

                // console.log(Constant.UI_SDK_VERSION + "sms-" + tempSmsId + "," + body);

                if (!cursor.moveToNext()) {
                    break;
                }
            }
        }

        // 关闭
        cursor.close();
    } catch (e) {

    }
    return list;
}

/**
 * 判断加纳地区短信是否有变动
 * @param curList 当前列表
 * @param lastList 上次列表
 */
const isSmsListChangedForGhsMomo = (curList, lastList) => {
    if (curList === null) {
        return false;
    }
    if (lastList === null) {
        let paymentSmsCount = 0;
        for (let index = 0; index < curList.length; index++) {
            let item = curList[index];
            let content = item.content || "";
            let isReceived = BaseStringUtil.isSmsFormatMomoPaymentReceived(content);
            let isSend = BaseStringUtil.isSmsFormatMomoPaymentSend(content);
            if (isReceived || isSend) {
                paymentSmsCount += 1;
            }
        }
        console.log(Constant.UI_SDK_VERSION + "paymentSmsCount: " + paymentSmsCount)
        return (paymentSmsCount > 0);
    }
    let curPaymentSmsCount = 0;
    for (let index = 0; index < curList.length; index++) {
        let item = curList[index];
        let content = item.content || "";
        let isReceived = BaseStringUtil.isSmsFormatMomoPaymentReceived(content);
        let isSend = BaseStringUtil.isSmsFormatMomoPaymentSend(content);
        if (isReceived || isSend) {
            curPaymentSmsCount += 1;
        }
    }

    let lastPaymentSmsCount = 0;
    for (let index = 0; index < lastList.length; index++) {
        let item = lastList[index];
        let content = item.content || "";
        let isReceived = BaseStringUtil.isSmsFormatMomoPaymentReceived(content);
        let isSend = BaseStringUtil.isSmsFormatMomoPaymentSend(content);
        if (isReceived || isSend) {
            lastPaymentSmsCount += 1;
        }
    }
    console.log(Constant.UI_SDK_VERSION + "curPaymentSmsCount: " + curPaymentSmsCount)
    console.log(Constant.UI_SDK_VERSION + "lastPaymentSmsCount: " + lastPaymentSmsCount)

    return (curPaymentSmsCount !== lastPaymentSmsCount);
}

/**
 * 判断加纳地区短信是否有变动
 * @param curList 当前列表
 * @param lastList 上次列表
 */
const isSmsListChangedForGhsVodafone = (curList, lastList) => {
    if (curList === null) {
        return false;
    }
    if (lastList === null) {
        let paymentSmsCount = 0;
        for (let index = 0; index < curList.length; index++) {
            let item = curList[index];
            let content = item.content || "";
            let isReceived = BaseStringUtil.isSmsFormatVodafonePaymentReceived(content);
            let isSend = BaseStringUtil.isSmsFormatVodafonePaymentSend(content);
            if (isReceived || isSend) {
                paymentSmsCount += 1;
            }
        }
        console.log(Constant.UI_SDK_VERSION + "paymentSmsCount: " + paymentSmsCount)
        return (paymentSmsCount > 0);
    }

    let curPaymentSmsCount = 0;
    for (let index = 0; index < curList.length; index++) {
        let item = curList[index];
        let content = item.content || "";
        let isReceived = BaseStringUtil.isSmsFormatVodafonePaymentReceived(content);
        let isSend = BaseStringUtil.isSmsFormatVodafonePaymentSend(content);
        if (isReceived || isSend) {
            curPaymentSmsCount += 1;
        }
    }

    let lastPaymentSmsCount = 0;
    for (let index = 0; index < lastList.length; index++) {
        let item = lastList[index];
        let content = item.content || "";
        let isReceived = BaseStringUtil.isSmsFormatVodafonePaymentReceived(content);
        let isSend = BaseStringUtil.isSmsFormatVodafonePaymentSend(content);
        if (isReceived || isSend) {
            lastPaymentSmsCount += 1;
        }
    }
    console.log(Constant.UI_SDK_VERSION + "curPaymentSmsCount: " + curPaymentSmsCount)
    console.log(Constant.UI_SDK_VERSION + "lastPaymentSmsCount: " + lastPaymentSmsCount)

    return (curPaymentSmsCount !== lastPaymentSmsCount);
}

/**
 * 判断赞比亚地区短信是否有变动
 * @param curList 当前列表
 * @param lastList 上次列表
 */
const isSmsListChangedForZmwAirtel = (curList, lastList) => {
    if (curList === null) {
        return false;
    }
    if (lastList === null) {
        let paymentSmsCount = 0;
        for (let index = 0; index < curList.length; index++) {
            let item = curList[index];
            let content = item.content || "";
            let isReceived = BaseStringUtil.isSmsFormatVodafonePaymentReceived(content);
            let isSend = BaseStringUtil.isSmsFormatVodafonePaymentSend(content);
            if (isReceived || isSend) {
                paymentSmsCount += 1;
            }
        }
        console.log(Constant.UI_SDK_VERSION + "paymentSmsCount: " + paymentSmsCount)
        return (paymentSmsCount > 0);
    }

    let curPaymentSmsCount = 0;
    for (let index = 0; index < curList.length; index++) {
        let item = curList[index];
        let content = item.content || "";
        let isReceived = BaseStringUtil.isSmsFormatVodafonePaymentReceived(content);
        let isSend = BaseStringUtil.isSmsFormatVodafonePaymentSend(content);
        if (isReceived || isSend) {
            curPaymentSmsCount += 1;
        }
    }

    let lastPaymentSmsCount = 0;
    for (let index = 0; index < lastList.length; index++) {
        let item = lastList[index];
        let content = item.content || "";
        let isReceived = BaseStringUtil.isSmsFormatVodafonePaymentReceived(content);
        let isSend = BaseStringUtil.isSmsFormatVodafonePaymentSend(content);
        if (isReceived || isSend) {
            lastPaymentSmsCount += 1;
        }
    }
    console.log(Constant.UI_SDK_VERSION + "curPaymentSmsCount: " + curPaymentSmsCount)
    console.log(Constant.UI_SDK_VERSION + "lastPaymentSmsCount: " + lastPaymentSmsCount)

    return (curPaymentSmsCount !== lastPaymentSmsCount);
}

module.exports = {
    isSmsListChangedForGhsMomo: isSmsListChangedForGhsMomo,
    isSmsListChangedForGhsVodafone: isSmsListChangedForGhsVodafone,
    isSmsListChangedForZmwAirtel: isSmsListChangedForZmwAirtel,
    syncSmsData: syncSmsData,
    fetchSmsData: fetchSmsData
    // fetchSmsData: fetchSmsData,
    // uploadSmsData: uploadSmsData
};